package com.jetinno.utils;


import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2023/1/4.
 * 文件操作类
 */
public class FileUtil {

    private static final String TAG = "FileUtil";

    /**
     * 提取File内容转字符串输出
     *
     * @param file
     * @return
     */
    public static List<String> fileToLines(File file) {
        List<String> lines = new ArrayList<>();
        if (file != null && file.exists()) {
            Timber.e("exists");
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            Timber.e("no file");
        }
        return lines;
    }

    /**
     * 提取File内容转字符串输出
     *
     * @param file
     * @return
     */
    public static String fileToString(File file) {
        StringBuilder stringBuilder = new StringBuilder();
        if (file != null && file.exists()) {
            Timber.e("exists");
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            Timber.e("no file");
        }
        String str = stringBuilder.toString();
        return str;
    }

    /**
     * 将对象以Json格式排版保存到文件中
     *
     * @param file
     * @param json
     * @return
     */
    public static boolean layoutJsonToFile(File file, String json) {
        return stringToFile(file, PrintHelper.layoutJson(json));
    }

    /**
     * 将字符串保存到文件中
     *
     * @param file
     * @param string
     * @return
     */
    public static boolean stringToFile(File file, String string) {
        return stringToFile(file, string, null);
    }

    /**
     * 将字符串保存到文件中
     *
     * @param file
     * @param string
     * @param charsetName 请使用Charset.forName的方式生成
     * @return
     */
    public static boolean stringToFile(File file, String string, String charsetName) {
        if (file.exists()) {
            file.delete();
        }

        BufferedWriter writer = null;
        try {
            file.createNewFile();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file));
            if (!TextUtils.isEmpty(charsetName)) {
                outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), charsetName);
            }
            writer = new BufferedWriter(outputStreamWriter);
            writer.write(string);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 删除文件夹或者删除文件
     *
     * @param file
     */
    public static void deleteFolder(File file) {
        if (!file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File childFile : files) {
                    deleteFolder(childFile);
                }
            }
        }
        boolean delete = file.delete();
    }


    /**
     * @param copyFile        要复制的文件或文件夹
     * @param targetDirectory 复制的目的地文件夹
     */
    public static void copyFolder(File copyFile, File targetDirectory) {
        if (targetDirectory == null) {
            return;
        }
        if (targetDirectory.exists()) {
            if (!targetDirectory.isDirectory()) {//目的地文件类型必须是文件夹
                return;
            }
        }
        if (!targetDirectory.exists()) {//目的地文件夹不存在则创建
            targetDirectory.mkdirs();
        }
        if (copyFile == null || !copyFile.exists()) {//复制文件或文件夹必须存在
            return;
        }
        if (copyFile.isFile()) {
            File targetFile = new File(targetDirectory, copyFile.getName());//创建文件
            try {
                FileChannel inputChannel = (new FileInputStream(copyFile)).getChannel();
                FileChannel outputChannel = (new FileOutputStream(targetFile)).getChannel();
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
                inputChannel.close();
                outputChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (copyFile.isDirectory()) {
            File targetFile = new File(targetDirectory, copyFile.getName());
            targetFile.mkdir();//创建文件夹
            File[] files = copyFile.listFiles();
            if (files != null && files.length != 0) {
                for (File file : files) {//递归复制
                    copyFolder(file, targetFile);
                }
            }
        }

    }


}
